 package com.zhq.blog;

import java.util.List;

import javax.servlet.Filter;
import javax.servlet.MultipartConfigElement;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.MultipartConfigFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.zhq.blog.manage.domain.controller.support.PermissionInterceptor;
import com.zhq.blog.manage.domain.fronted.controller.support.UserInterceptor;

@Configuration
@EnableAutoConfiguration
@ComponentScan
@Transactional
@EnableJpaAuditing
@ImportResource("classpath:applicationContext.xml")
public class Application extends WebMvcConfigurerAdapter {
	
	@Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
    }
	
	/**
	 * 增加内置容器对utf-8系统的过滤器
	 * 如果没有这个过滤器，tomcat下回出现乱码，jetty没有太大问题
	 *
	 */
	@Configuration
	protected static class ApplicationContextFilterConfiguration {
		@Bean
		public Filter applicationContextIdFilter(ApplicationContext context) {
			CharacterEncodingFilter filter = new CharacterEncodingFilter();
			filter.setEncoding("UTF-8");
			return filter;
		}
	}		
	
	/**
	 * 开发时使用内嵌数据库名称
	 */
	@Value("${spring.datasource.name:testdb}")
	private String name = "testdb";
	
	/**
	 * spring mvc拦截器定义  后台
	 * @return 用于权限管理的拦截器
	 */
	@Bean
	public PermissionInterceptor permissionInterceptor() {
	    return new PermissionInterceptor();
	}
	
	@Bean
	public UserInterceptor userInterceptor(){
		return new UserInterceptor();
	}

	/**
	 * 为springboot增加spring mvc拦截器定义
	 */
	@Override
	public void addInterceptors(InterceptorRegistry registry) {
		registry.addInterceptor(permissionInterceptor());
		registry.addInterceptor(userInterceptor());
	}
	
	@Bean
    MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        factory.setMaxFileSize("4096KB");
        factory.setMaxRequestSize("4096KB");
        return factory.createMultipartConfig();
    }	
    public static void main(String[] args) {
    	SpringApplication app = new SpringApplication(Application.class);
        app.setShowBanner(true); 
        app.run(args);
    }
}
